In [37]:
%matplotlib inline
from __future__ import division
fs = 8000.0
f0 = 256.0 # Hz
duration = 0.05 # 1.0
t = np.linspace(0, duration, num=fs*duration)
N_overtones = 7
harmonics = np.arange(1, N_overtones)
f = f0 * harmonics;
print f
# exponentially decaying gain
G = 0.5 * np.exp(-f/1e3) + 0.5
# G = np.ones_like(f)
x_full = np.sum(np.exp(2j*np.pi*np.outer(f,t)), 0)/N_overtones
x_missing1 = np.sum(G[1:]*np.exp(2j*np.pi*np.outer(t, f[1:])), 1)/N_overtones
x_missing2 = np.sum(G[2:]*np.exp(2j*np.pi*np.outer(t, f[2:])), 1)/N_overtones
x_missing3 = np.sum(G[3:]*np.exp(2j*np.pi*np.outer(t, f[3:])), 1)/N_overtones
from scipy.io import wavfile
plt.figure(figsize=(16,4))
ax1 = plt.subplot(1,4,1)
plt.plot(t, np.real(x_full))
ax2 = plt.subplot(1,4,2, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing1))
ax3 = plt.subplot(1,4,3, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing2))
ax4 = plt.subplot(1,4,4, sharex=ax1, sharey=ax1)
plt.plot(t, np.real(x_missing3))
plt.xlim(0, 0.02)
plt.ylim(-1, 1)
Out[37]:
In [23]:
from IPython.display import Audio
Audio(np.real(x_full), rate=fs)
Out[23]:
In [24]:
Audio(np.real(x_missing1), rate=fs)
Out[24]:
In [25]:
Audio(np.real(x_missing2), rate=fs)
Out[25]:
In [26]:
Audio(np.real(x_missing3), rate=fs)
Out[26]:
In [5]:
from pygrfnn import Zparam, GrFNN
from pygrfnn.network import Model, make_connections
from pygrfnn.vis import tf_detail, tf_simple
# params = Zparam(alpha=0.02, beta1=0.0, beta2=.0, epsilon=1.0)
# params = Zparam(alpha=0.01, beta1=-15.0, beta2=-5, epsilon=1.0)
# params = Zparam(alpha=-0.5, beta1=10.0, beta2=-3.0, epsilon=1.0)
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
# params2 = Zparam(alpha=-0.01, beta1=5, beta2=-3.0, epsilon=1.0)
params2 = Zparam(alpha=-0.2, beta1=0, beta2=0.0, epsilon=1.0)
# freqs = (128, 2048)
# num_oscs = 256
freqs = (128, 2048)
num_oscs = 256
stimulus_conn_type = 'linear' # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2
# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'
In [10]:
# create model
g = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
mf = Model()
mf.add_layer(g, input_channel=0)
M = make_connections(g, g, modes=[1/3, 1/2, 1, 2, 3])
# mf.connect_layers(g, g, M, connection_type=intra_layer_conn_type, self_connect=False)
# print mf.connections[g][0].monomials[64].indices.shape
# run it
mf.run(gain*x_full, t, 1/fs)
# show TFR
tf_detail(g.Z, t, g.f, t_detail=0.08, title="All Harmonics")
Out[10]:
In [28]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.2, beta1=0, beta2=0.0, epsilon=1.0)
# freqs = (128, 2048)
# num_oscs = 256
freqs = (128, 2048)
num_oscs = 256
stimulus_conn_type = 'linear' # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.4
# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'
# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)
M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
M2 = make_connections(g2, g2)
m1.connect_layers(g1, g2, M1, weight=500, connection_type=intra_layer_conn_type, self_connect=True)
m1.connect_layers(g2, g2, M2, weight=-10, connection_type='all2freq', self_connect=True)
# run it
m1.run(gain*x_missing1, t, 1/fs)
# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")
In [51]:
plt.figure(figsize=(14,6))
ax1 = plt.subplot(1,2,1)
ax1.semilogx(g2.f, np.abs(g2.Z[:,-1]), 'b', label="layer2")
ax1.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
for tl in ax1.get_yticklabels():
tl.set_color('b')
ax2 = ax1.twinx()
ax2.semilogx(g1.f, np.abs(g1.Z[:,-1]), 'g' ,label="layer1")
for tl in ax2.get_yticklabels():
tl.set_color('g')
ax1.axis('tight')
plt.subplot(1,2,2)
plt.semilogx(g2.f, np.abs(g2.Z[:,-1]))
plt.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
plt.xlim(200, 300)
Out[51]:
In [20]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)
# freqs = (128, 2048)
# num_oscs = 256
freqs = (128, 2048)
num_oscs = 257
stimulus_conn_type = 'linear' # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2
# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'
# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)
M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)
m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)
# run it
m1.run(gain*x_missing1, t, 1/fs)
# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")
Out[20]:
In [14]:
# plt.plot(t, np.abs(g2.Z[64,:]))
tf_detail(g2.Z, t, g2.f, t_detail=[0.03, 0.04, 0.05], title="Missing fundamental")
Out[14]:
In [15]:
plt.figure(figsize=(14,6))
ax1 = plt.subplot(1,2,1)
ax1.semilogx(g2.f, np.abs(g2.Z[:,-1]), 'b', label="layer2")
ax1.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
for tl in ax1.get_yticklabels():
tl.set_color('b')
ax2 = ax1.twinx()
ax2.semilogx(g1.f, np.abs(g1.Z[:,-1]), 'g' ,label="layer1")
for tl in ax2.get_yticklabels():
tl.set_color('g')
ax1.axis('tight')
plt.subplot(1,2,2)
plt.semilogx(g2.f, np.abs(g2.Z[:,-1]))
plt.semilogx(g2.f[64], np.abs(g2.Z[64,-1]), 'r.')
plt.xlim(200, 300)
Out[15]:
In [29]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)
# freqs = (128, 2048)
# num_oscs = 256
freqs = (128, 2048)
num_oscs = 257
stimulus_conn_type = 'linear' # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2
# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'
# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)
M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)
m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)
# run it
m1.run(gain*x_missing2, t, 1/fs)
# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")
Out[29]:
In [38]:
params1 = Zparam(alpha=-0.02, beta1=0, beta2=0, epsilon=1.0)
params2 = Zparam(alpha=-0.5, beta1=0.5, beta2=-0.1, epsilon=1.0)
# freqs = (128, 2048)
# num_oscs = 256
freqs = (128, 2048)
num_oscs = 257
stimulus_conn_type = 'linear' # 'linear', 'active', 'allfreq', 'all2freq'
gain = 0.2
# intra_layer_conn_type = 'all2freq'
intra_layer_conn_type = '3freq'
# create model
g1 = GrFNN(params1, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
g2 = GrFNN(params2, frequency_range=freqs, num_oscs=num_oscs, stimulus_conn_type=stimulus_conn_type)
m1 = Model()
m1.add_layer(g1, input_channel=0)
m1.add_layer(g2)
M1 = make_connections(g1, g1)
# M1 = np.ones_like(M1)
# m1.connect_layers(g1, g2, M1, weight=25, connection_type=intra_layer_conn_type, self_connect=True)
# M2 = make_connections(g2, g2)
m1.connect_layers(g1, g2, M1, weight=100, connection_type=intra_layer_conn_type, self_connect=True)
# m1.connect_layers(g2, g1, M2, weight=1, connection_type='all2freq', self_connect=True)
# run it
m1.run(gain*x_missing3, t, 1/fs)
# show TFR
tf_detail(g1.Z, t, g1.f, t_detail=np.max(t), title="Missing fundamental")
tf_detail(g2.Z, t, g2.f, t_detail=np.max(t), title="Missing fundamental")
Out[38]:
In [ ]: